<template>
  <div class="register">
    <div class="wrapper">
      <div class="left-wrapper">
        <img :src="registerImg" alt="" />
      </div>
      <div class="right-wrapper">
        <!-- 标题 -->
        <p class="title">访客注册</p>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="auto"
          class="demo-ruleForm"
        >
          <el-form-item label="账户:" prop="username">
            <el-input
              v-model="ruleForm.username"
              size="small"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码:" prop="password">
            <el-input
              v-model="ruleForm.password"
              size="small"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="确认:" prop="againPasswrod">
            <el-input
              v-model="ruleForm.againPasswrod"
              size="small"
              placeholder="请再次输入密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >注册</el-button
            >
          </el-form-item>
        </el-form>
        <p class="login" @click="toLogin()">Or 去登陆</p>
      </div>
    </div>
  </div>
</template>
<script>
import registerImg from "../../assets/login.png";
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
        againPasswrod: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        againPasswrod: [
          { required: true, message: "请输入密码", trigger: "blur" },
        ],
      },
      registerImg,
    };
  },
  computed: {},
  methods: {
    // 点击注册
    submitForm() {
      if (
        this.ruleForm.username &&
        this.ruleForm.password &&
        this.ruleForm.againPasswrod
      ) {
        if (this.ruleForm.password === this.ruleForm.againPasswrod) {
          this.$api
            .register({
              name: this.ruleForm.username,
              password: this.ruleForm.password,
            })
            .then((res) => {
              if (res.success) {
                this.$message({
                  message: "注册成功",
                  type: "success",
                });
                // 跳转到登录页
                this.$router.push("/login");
              } else {
                this.$message({
                  message: "注册失败",
                  type: "warning",
                });
              }
            });
        } else {
          this.$message({
            message: "两次密码输入不一样",
            type: "warning",
          });
        }
      } else {
        this.$message({
          message: "账户名密码不能输入为空",
          type: "warning",
        });
      }
    },
    // 点击去注册
    toLogin() {
      this.$router.push("/login");
    },
  },
  components: {},
};
</script>

<style lang="scss" scoped>
.register {
  width: 100%;
  height: 100%;
  background-color: #ccc;
  display: flex;
  align-items: center;
  justify-content: center;
}
.wrapper {
  width: 1240px;
  height: 627px;
  background-color: #fff;
  display: flex;
  .left-wrapper {
    width: 50%;
    height: 100%;
    img {
      width: 100%;
    }
  }
  .right-wrapper {
    width: 50%;
    height: 100%;
    background-color: #f7fafb;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 20px;
    .title {
      font-size: 20px;
      height: 30px;
      line-height: 30px;
      padding: 0 10px;
    }
    .el-form {
      width: 100%;
      .el-button {
        width: 100%;
        height: 30px;
        padding: 0;
        background-color: #0188fb;
      }
      .el-button:hover {
        background-color: #29a2ff;
      }
    }
    .login {
      padding: 0 20px;
    }
  }
}
</style>